package bibliotheque;

import bibliotheque.client.gui.ClientGui;
import bibliotheque.events.ErrorOccuredEvent;
import bibliotheque.events.ErrorOccuredListener;
import bibliotheque.events.StateChangedEvent;
import bibliotheque.events.StateChangedListener;
import bibliotheque.nameserver.NameServer;
import bibliotheque.server.Server;
import java.io.File;
import java.util.Date;

public class Main extends Thread implements ErrorOccuredListener, StateChangedListener{
    
    public Main() {
        
    }
    
    public void run() {
        try {
            RMIRegistry.Start(2222);
            /*
            new NameServerGui().setVisible(true);
            new ServerGui().setVisible(true);
            new ServerGui().setVisible(true);
            new ServerGui().setVisible(true);
            */
            
            NameServer annuaire = new NameServer();
            Server server1 = new Server("192.168.0.1");
            Server server2 = new Server("192.168.0.2");
            Server server3 = new Server("192.168.0.3");
            Server server4 = new Server("192.168.0.4");
            Server server5 = new Server("192.168.0.5");
            Server server6 = new Server("192.168.0.6");
            Server server7 = new Server("192.168.0.7");
            Server server8 = new Server("192.168.0.8");
            Server server9 = new Server("192.168.0.9");
            Server server10 = new Server("192.168.0.10");
            Server server11 = new Server("192.168.0.11");
            Server server12 = new Server("192.168.0.12");
            Server server13 = new Server("192.168.0.13");
            Server server14 = new Server("192.168.0.14");
            Server server15 = new Server("192.168.0.15");
            Server server16 = new Server("192.168.0.16");
            Server server17 = new Server("192.168.0.17");
            Server server18 = new Server("192.168.0.18");
            Server server19 = new Server("192.168.0.19");
            Server server20 = new Server("192.168.0.20");
            Server server21 = new Server("192.168.0.21");
            Server server22 = new Server("192.168.0.22");
            Server server23 = new Server("192.168.0.23");
            Server server24 = new Server("192.168.0.24");
            Server server25 = new Server("192.168.0.25");
            Server server26 = new Server("192.168.0.26");
            Server server27 = new Server("192.168.0.27");
            Server server28 = new Server("192.168.0.28");
            Server server29 = new Server("192.168.0.29");
            Server server30 = new Server("192.168.0.30");
            Server server31 = new Server("192.168.0.31");
            Server server32 = new Server("192.168.0.32");
            Server server33 = new Server("192.168.0.33");
            Server server34 = new Server("192.168.0.34");
            Server server35 = new Server("192.168.0.35");
            Server server36 = new Server("192.168.0.36");
            Server server37 = new Server("192.168.0.37");
            Server server38 = new Server("192.168.0.38");
            Server server39 = new Server("192.168.0.39");
            Server server40 = new Server("192.168.0.40");
            Server server41 = new Server("192.168.0.41");
            Server server42 = new Server("192.168.0.42");
            Server server43 = new Server("192.168.0.43");
            Server server44 = new Server("192.168.0.44");
            Server server45 = new Server("192.168.0.45");
            Server server46 = new Server("192.168.0.46");
            Server server47 = new Server("192.168.0.47");
            Server server48 = new Server("192.168.0.48");
            Server server49 = new Server("192.168.0.49");
            Server server50 = new Server("192.168.0.50");
            annuaire.AddErrorOccuredListener(this);
            annuaire.AddStateChangedListener(this);
            server1.AddErrorOccuredListener(this);
            server1.AddStateChangedListener(this);
            server2.AddErrorOccuredListener(this);
            server2.AddStateChangedListener(this);
            server3.AddErrorOccuredListener(this);
            server3.AddStateChangedListener(this);
            server4.AddErrorOccuredListener(this);
            server4.AddStateChangedListener(this);
            server5.AddErrorOccuredListener(this);
            server5.AddStateChangedListener(this);
            server6.AddErrorOccuredListener(this);
            server6.AddStateChangedListener(this);
            server7.AddErrorOccuredListener(this);
            server7.AddStateChangedListener(this);
            server8.AddErrorOccuredListener(this);
            server8.AddStateChangedListener(this);
            server9.AddErrorOccuredListener(this);
            server9.AddStateChangedListener(this);
            server10.AddErrorOccuredListener(this);
            server10.AddStateChangedListener(this);
            server11.AddErrorOccuredListener(this);
            server11.AddStateChangedListener(this);
            server12.AddErrorOccuredListener(this);
            server12.AddStateChangedListener(this);
            server13.AddErrorOccuredListener(this);
            server13.AddStateChangedListener(this);
            server14.AddErrorOccuredListener(this);
            server14.AddStateChangedListener(this);
            server15.AddErrorOccuredListener(this);
            server15.AddStateChangedListener(this);
            server16.AddErrorOccuredListener(this);
            server16.AddStateChangedListener(this);
            server17.AddErrorOccuredListener(this);
            server17.AddStateChangedListener(this);
            server18.AddErrorOccuredListener(this);
            server18.AddStateChangedListener(this);
            server19.AddErrorOccuredListener(this);
            server19.AddStateChangedListener(this);
            server20.AddErrorOccuredListener(this);
            server20.AddStateChangedListener(this);
            server21.AddErrorOccuredListener(this);
            server21.AddStateChangedListener(this);
            server22.AddErrorOccuredListener(this);
            server22.AddStateChangedListener(this);
            server23.AddErrorOccuredListener(this);
            server23.AddStateChangedListener(this);
            server24.AddErrorOccuredListener(this);
            server24.AddStateChangedListener(this);
            server25.AddErrorOccuredListener(this);
            server25.AddStateChangedListener(this);
            server26.AddErrorOccuredListener(this);
            server26.AddStateChangedListener(this);
            server27.AddErrorOccuredListener(this);
            server27.AddStateChangedListener(this);
            server28.AddErrorOccuredListener(this);
            server28.AddStateChangedListener(this);
            server29.AddErrorOccuredListener(this);
            server29.AddStateChangedListener(this);
            server30.AddErrorOccuredListener(this);
            server30.AddStateChangedListener(this);
            server31.AddErrorOccuredListener(this);
            server31.AddStateChangedListener(this);
            server32.AddErrorOccuredListener(this);
            server32.AddStateChangedListener(this);
            server33.AddErrorOccuredListener(this);
            server33.AddStateChangedListener(this);
            server34.AddErrorOccuredListener(this);
            server34.AddStateChangedListener(this);
            server35.AddErrorOccuredListener(this);
            server35.AddStateChangedListener(this);
            server36.AddErrorOccuredListener(this);
            server36.AddStateChangedListener(this);
            server37.AddErrorOccuredListener(this);
            server37.AddStateChangedListener(this);
            server38.AddErrorOccuredListener(this);
            server38.AddStateChangedListener(this);
            server39.AddErrorOccuredListener(this);
            server39.AddStateChangedListener(this);
            server40.AddErrorOccuredListener(this);
            server40.AddStateChangedListener(this);
            server41.AddErrorOccuredListener(this);
            server41.AddStateChangedListener(this);
            server42.AddErrorOccuredListener(this);
            server42.AddStateChangedListener(this);
            server43.AddErrorOccuredListener(this);
            server43.AddStateChangedListener(this);
            server44.AddErrorOccuredListener(this);
            server44.AddStateChangedListener(this);
            server45.AddErrorOccuredListener(this);
            server45.AddStateChangedListener(this);
            server46.AddErrorOccuredListener(this);
            server46.AddStateChangedListener(this);
            server47.AddErrorOccuredListener(this);
            server47.AddStateChangedListener(this);
            server48.AddErrorOccuredListener(this);
            server48.AddStateChangedListener(this);
            server49.AddErrorOccuredListener(this);
            server49.AddStateChangedListener(this);
            server50.AddErrorOccuredListener(this);
            server50.AddStateChangedListener(this);
            annuaire.StartService(new File("src/bibliotheque/config/the_big_one.xnc"), "bibliotheque", "127.0.0.1", 2222);
            server1.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server2.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server3.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server4.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server5.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server6.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server7.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server8.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server9.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server10.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server11.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server12.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server13.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server14.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server15.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server16.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server17.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server18.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server19.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server20.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server21.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server22.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server23.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server24.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server25.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server26.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server27.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server28.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server29.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server30.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server31.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server32.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server33.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server34.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server35.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server36.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server37.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server38.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server39.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server40.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server41.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server42.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server43.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server44.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server45.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server46.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server47.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server48.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server49.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server50.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            /*ClientGui client = new ClientGui();
            client.setVisible(true);
            while(true);*/
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    
    public void ErrorOccured(ErrorOccuredEvent event) {
        Server server;
        if(event.getSource() instanceof NameServer) {
            System.out.println("nameServer : " + event.GetError() + "  " + (new Date()).toString());
        }
        else if(event.getSource() instanceof Server) {
            server = (Server)event.getSource();
            System.out.println(server.GetIp() + " : " + event.GetError() + "  " + (new Date()).toString());
        }
    }
    
    public void StateChanged(StateChangedEvent event) {
        Server server;
        if(event.getSource() instanceof NameServer) {
            System.out.println("nameServer : " + event.GetState() + "  " + (new Date()).toString());
        }
        else if(event.getSource() instanceof Server) {
            server = (Server)event.getSource();
            System.out.println(server.GetIp() + " : " + event.GetState() + "  " + (new Date()).toString());
        }
    }

    public static void main(String[] args) {
         new Main().start();
    }
}
